﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kwt.ctk
{
    using System.IO;
    using System.Text.RegularExpressions;
    using kwt.core;
    using System.Threading;

    #region Class Definition
    public partial class MainForm : Form
    {
        delegate string[] GetParentFilesCallback(string path);
        delegate void AppendTextCallback(string msg);
        GetParentFilesCallback method = null;
        GetParentFilesCallback method2 = null;
        string path = string.Empty;
        string[] results;

        #region Initialize

        public MainForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            // 在窗口名称处显示版本号
            this.Text = string.Format(
                this.Text,
                this.ProductVersion);

            // 显示图标
            this.Icon = Properties.Resources.app;
            this.AllowTransparency = true;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        #endregion

        #region Drag&Drop Handles

        private void OnFile_DragOver(object sender, DragEventArgs e)
        {
            if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void OnFile_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Effect == DragDropEffects.Link)
            {
                if (e.Data.GetData(DataFormats.FileDrop) != null)
                {
                    path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                    string ext = Path.GetExtension(path).ToLower();
                    if (ext != ".css")
                    {
                        MessageBox.Show(
                            "只处理样式文件！",
                            "出错了！",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                    else
                    {
                        AppendText("\r\n正在处理中，请稍候...\r\n");
                        method = new GetParentFilesCallback(IOHelper.ParentFiles);
                        method.BeginInvoke(path, new AsyncCallback(WorkFinished), null);
                        method2 = new GetParentFilesCallback(IOHelper.ChildFiles);
                        method2.BeginInvoke(path, new AsyncCallback(WorkFinished2), null);
                        
                    }
                }
            }
        }

        #endregion

        private void WorkFinished(IAsyncResult result)
        {
            results = method.EndInvoke(result);
            if (results.Length <= 0)
            {
                AppendText("\r\n没有文件引用当前样式\r\n");
                AppendText(new String('=', 25));
                return;
            }
            StringBuilder sb = new StringBuilder("\r\n" + Path.GetFileName(path)+"被以下文件引用：\r\n");
            foreach (string file in results)
            {
                sb.AppendLine("    " + file);
            }
            sb.Append(new string('=', 25));
            AppendText(sb.ToString());

        }

        private void WorkFinished2(IAsyncResult result)
        {
            results = method2.EndInvoke(result);
            if (results.Length <= 0)
            {
                AppendText("\r\n没有文件被当前样式文件引用\r\n");
                AppendText(new String('=', 25));
                return;
            }
            StringBuilder sb = new StringBuilder("\r\n" + Path.GetFileName(path) + "\r\n");
            foreach (string file in results)
            {
                sb.AppendLine("    |--" + file);
            }
            sb.Append(new string('=', 25));
            AppendText(sb.ToString());

        }

        private void AppendText(string msg)
        {
            if (this.rtbResult.InvokeRequired)
            {
                AppendTextCallback a = new AppendTextCallback(AppendText);
                this.Invoke(a, new object[] { msg });
            }
            else
            {
                this.rtbResult.AppendText(msg);
            }
        }

        private void btnChooseRoot_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.ShowNewFolderButton = false;
            fd.RootFolder = Environment.SpecialFolder.MyComputer;
            DialogResult dr = fd.ShowDialog(this);
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                this.tbWorkRoot.Text = fd.SelectedPath;
            }
        }

        private void cbTopMost_CheckedChanged(object sender, EventArgs e)
        {
            this.TopMost = this.cbTopMost.Checked;
        }

        private void cbTransparent_CheckedChanged(object sender, EventArgs e)
        {
            this.Opacity = this.cbTransparent.Checked ? 0.75 : 1;
        }
    }
#endregion
}
